Ext.define('SKJF.controller.SALESRTN',{
	extend: 'Ext.app.Controller',
	views: ['PENJUALAN.v_salesrtn','PENJUALAN.v_salesrtn_form'],
	models: ['m_salesrtn'],
	stores: ['s_salesrtn'],
	
	requires: ['Ext.ModelManager'],
	
	refs: [{
		ref: 'Listsalesrtn',
		selector: 'Listsalesrtn'
	}, {
		ref: 'Listsalesrtndet',
		selector: 'Listsalesrtndet'
	}, {
		ref: 'v_salesrtn_form',
		selector: 'v_salesrtn_form'
	}, {
		ref: 'Savesalesrtn',
		selector: 'v_salesrtn_form #save'
	}, {
		ref: 'Createsalesrtn',
		selector: 'v_salesrtn_form #create'
	}, {
		ref: 'SALESRTN',
		selector: 'SALESRTN #center'
	}],


	init: function(){
		this.control({
			'SALESRTN': {
				'afterrender': this.salesrtnAfterRender
			},
			'v_salesrtn_form': {
				'afterlayout': this.salesrtnAfterLayout
			},
			'Listsalesrtn': {
				'selectionchange': this.enableDelete,
				'itemdblclick': this.updateListsalesrtn
			},
			'Listsalesrtn button[action=create]': {
				click: this.createRecord
			},
			'Listsalesrtn button[action=delete]': {
				click: this.deleteRecord
			},
			'Listsalesrtn button[action=print]': {
				click: this.printRecords
			},
			'v_salesrtn_form button[action=save]': {
				click: this.saveV_salesrtn_form
			},
			'v_salesrtn_form button[action=create]': {
				click: this.saveV_salesrtn_form
			},
			'v_salesrtn_form button[action=cancel]': {
				click: this.cancelV_salesrtn_form
			}
		});
	},
	
	salesrtnAfterRender: function(){
		var salesrtnStore = this.getListsalesrtn().getStore();
		salesrtnStore.load();
	},
	
	salesrtnAfterLayout: function(){this.getV_salesrtn_form().down('#NOMOR_SALES_RETURN_field').focus(false, true);
	},
	
	createRecord: function(){
		var getListsalesrtn	= this.getListsalesrtn();
		var getV_salesrtn_form= this.getV_salesrtn_form(),
			form			= getV_salesrtn_form.getForm();
		var getSavesalesrtn	= this.getSavesalesrtn();
		var getCreatesalesrtn	= this.getCreatesalesrtn();
		
		/* grid-panel */
		getListsalesrtn.setDisabled(true);
        
		/* form-panel */
		form.reset();
		getV_salesrtn_form.down('#NOMOR_SALES_RETURN_field').setReadOnly(false);
		getSavesalesrtn.setDisabled(true);
		getCreatesalesrtn.setDisabled(false);
		getV_salesrtn_form.setDisabled(false);
		
		this.getSALESRTN().setActiveTab(getV_salesrtn_form);		
	},

	enableDelete: function(dataview, selections){
		if (selections.length) {
			var sel = selections[0].data;
			// console.info('Sel : '+sel.NOMOR_SALES_RETURN);
			this.getListsalesrtn().down('#btndelete').setDisabled(!selections.length);
			this.getListsalesrtndet().down('#btncreate').setDisabled(false);
			this.getListsalesrtndet().down('#btnprint').setDisabled(false);
			
			this.getListsalesrtndet().getStore().load({
				params: {
					NOMOR_SALES_RETURN: sel.NOMOR_SALES_RETURN
				}
			});
			
		}else{
			this.getListsalesrtn().down('#btndelete').setDisabled(!selections.length);
			this.getListsalesrtndet().down('#btncreate').setDisabled(true);
			this.getListsalesrtndet().down('#btndelete').setDisabled(true);
			this.getListsalesrtndet().down('#btnprint').setDisabled(true);
			this.getListsalesrtndet().getStore().removeAll();
		}		
	},
	
	updateListsalesrtn: function(me, record, item, index, e){
		var getSALESRTN		= this.getSALESRTN();
		var getListsalesrtn	= this.getListsalesrtn();
		var getV_salesrtn_form= this.getV_salesrtn_form(),
			form			= getV_salesrtn_form.getForm();
		var getSavesalesrtn	= this.getSavesalesrtn();
		var getCreatesalesrtn	= this.getCreatesalesrtn();
		
		getSavesalesrtn.setDisabled(false);
		getCreatesalesrtn.setDisabled(true);
		getV_salesrtn_form.down('#NOMOR_SALES_RETURN_field').setReadOnly(true);		
		getV_salesrtn_form.loadRecord(record);
		
		getListsalesrtn.setDisabled(true);
		getV_salesrtn_form.setDisabled(false);
		getSALESRTN.setActiveTab(getV_salesrtn_form);
	},
	
	deleteRecord: function(dataview, selections){
		var getstore = this.getListsalesrtn().getStore();
		var selection = this.getListsalesrtn().getSelectionModel().getSelection()[0];
		if(selection){
			Ext.Msg.confirm('Confirmation', 'Are you sure to delete this data: "NOMOR_SALES_RETURN" = "'+selection.data.NOMOR_SALES_RETURN+'","ID" = "'+selection.data.ID+'"?', function(btn){
				if (btn == 'yes'){
					getstore.remove(selection);
					getstore.sync();
				}
			});
			
		}
	},
	
	printRecords: function(){
		var getstore = this.getListsalesrtn().getStore();
		var jsonData = Ext.encode(Ext.pluck(getstore.data.items, 'data'));
		
		Ext.Ajax.request({
			method: 'POST',
			url: 'c_salesrtn/printRecords',
			params: {data: jsonData},
			success: function(response){
				var result=eval(response.responseText);
				switch(result){
				case 1:
					win = window.open('./temp/salesrtn.html','salesrtn_list','height=400,width=900,resizable=1,scrollbars=1, menubar=1');
					break;
				default:
					Ext.MessageBox.show({
						title: 'Warning',
						msg: 'Unable to print the grid!',
						buttons: Ext.MessageBox.OK,
						animEl: 'save',
						icon: Ext.MessageBox.WARNING
					});
					break;
				}  
			}
		});
	},
	
	saveV_salesrtn_form: function(){
		var getSALESRTN		= this.getSALESRTN();
		var getListsalesrtn 	= this.getListsalesrtn();
		var getV_salesrtn_form= this.getV_salesrtn_form(),
			form			= getV_salesrtn_form.getForm(),
			values			= getV_salesrtn_form.getValues();
		var store 			= this.getStore('s_salesrtn');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_salesrtn/save',
				params: {data: jsonData},
				success: function(response){
					store.reload({
						callback: function(){
							var newRecordIndex = store.findBy(
								function(record, id) {
									if (record.get('NOMOR_SALES_RETURN') === values.NOMOR_SALES_RETURN) {
										return true;
									}
									return false;
								}
							);
							/* getListsalesrtn.getView().select(recordIndex); */
							getListsalesrtn.getSelectionModel().select(newRecordIndex);
						}
					});
					
					getV_salesrtn_form.setDisabled(true);
					getListsalesrtn.setDisabled(false);
					getSALESRTN.setActiveTab(getListsalesrtn);
				}
			});
		}
	},
	
	createV_salesrtn_form: function(){
		var getSALESRTN		= this.getSALESRTN();
		var getListsalesrtn 	= this.getListsalesrtn();
		var getV_salesrtn_form= this.getV_salesrtn_form(),
			form			= getV_salesrtn_form.getForm(),
			values			= getV_salesrtn_form.getValues();
		var store 			= this.getStore('s_salesrtn');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_salesrtn/save',
				params: {data: jsonData},
				success: function(response){
					store.reload();
					
					getV_salesrtn_form.setDisabled(true);
					getListsalesrtn.setDisabled(false);
					getSALESRTN.setActiveTab(getListsalesrtn);
				}
			});
		}
	},
	
	cancelV_salesrtn_form: function(){
		var getSALESRTN		= this.getSALESRTN();
		var getListsalesrtn	= this.getListsalesrtn();
		var getV_salesrtn_form= this.getV_salesrtn_form(),
			form			= getV_salesrtn_form.getForm();
			
		form.reset();
		getV_salesrtn_form.setDisabled(true);
		getListsalesrtn.setDisabled(false);
		getSALESRTN.setActiveTab(getListsalesrtn);
	}
	
});